<template>
    <div id="mainpage-aside">
        <div class="mainpage-aside-header">
            菜单导航栏
        </div>
        <div class="mainpage-aside-container">
            <el-menu :defaultActive="activeTab" active-text-color="#f5d970" :router="false">
                <menu-tree v-for="item of menu_items" :key="item.path" :menu_items="item"></menu-tree>
            </el-menu>
        </div>
        <div v-if="isAdmin" class="mainpage-aside-footer" :key="1">
            <el-popover placement="right-start" width="200" trigger="hover">
                <ul class="mainpage-aside-footer-ul">
                    <li @click="showDrawer">配置菜单栏</li>
                </ul>
                <div slot="reference">开发者菜单</div>
            </el-popover>
        </div>
        <el-drawer title="配置菜单栏及其权限" :visible.sync="visible_drawer" direction="btt" size="80%" :destroy-on-close="true"
            :show-close="true">
            <new-menu ref="new_menu"></new-menu>
        </el-drawer>
    </div>
</template>

<script>
    import { mapState } from 'vuex'
    import menuTree from '@/components/common/menuTree.vue'
    import newMenu from '@/components/common/new_menu.vue'
    export default {
        name: 'menuTag',
        components: {
            menuTree,
            newMenu
        },
        data() {
            return {
                visible_drawer: false,
                menu_items: [],
                accountInfo: JSON.parse(localStorage.getItem('HuiLY')).userinfo,
                isAdmin: false
            }
        },
        computed: {
            // ...mapState(['activeTab', 'menu_items'])
            ...mapState(['activeTab'])
        },
        methods: {
            showDrawer() {
                this.visible_drawer = true
                this.$nextTick(() => {
                    this.$refs.new_menu.GetMenuList();
                })
            },
            // 获取菜单栏
            GetMenuList() {
                let loading = this.$loading({
                    target: '#mainpage-aside',
                    text: '菜单加载中'
                })
                this.$GET('/menu/getMenuList', { isAdmin: this.isAdmin })
                    .then((res) => {
                        this.menu_items = res.data.data
                        loading.close()
                    }).catch((res) => {
                        this.$message({
                            message: res.data.msg,
                            duration: 1000,
                            type: 'error'
                        })
                        loading.close()
                    }).finally(_ => {
                        setTimeout(() => {
                            loading.close()
                        }, 200)
                    })
            }
        },
        mounted() {
            this.$BUS.$on('menuAlter', () => {
                this.GetMenuList()
            })
            this.$IsAdmin(this.accountInfo).then((res) => {
                this.isAdmin = res.data.data
                this.GetMenuList()
            }).catch((res) => {
                this.isAdmin = res.data.data
                this.GetMenuList()
            })
        }
    }
</script>

<style scoped lang="less">
    #mainpage-aside {
        height: 100%;
        width: 100%;
        display: flex;
        flex: 1;
        flex-direction: column;

        .mainpage-aside-header {
            width: 100%;
            height: 60px;
            line-height: 60px;
            font-size: 25px;
            border-bottom: 1px solid rgb(230, 230, 240);
        }

        .mainpage-aside-container {
            width: 100%;
            text-align: left;
            flex-grow: 1;
        }

        .mainpage-aside-footer {
            width: 100%;
            height: 30px;
            line-height: 30px;
            font-size: 20px;
            align-items: flex-end;
            border-top: 1px solid rgb(230, 230, 240);

            &-ul {
                display: flex;
                justify-content: center;
                flex-direction: column;
                align-items: center;

                li {
                    display: block;
                    text-align: center;
                    width: 100%;

                    &:hover {
                        cursor: pointer;
                    }
                }
            }
        }
    }
</style>